# Make command to use for dependencies
MAKECMD=gmake

# If no configuration is specified, "Debug" will be used
ifndef "CFG"
CFG=Release
endif

#
# Configuration: Debug
#
ifeq "$(CFG)" "Debug"
OUTDIR=Debug
OUTFILE=$(OUTDIR)/chroma
CFG_INC=
CFG_LIB=-lstdc++
CFG_OBJ=
COMPILE=g++ -c  -g -Wall -o "$(OUTDIR)/$(*F).o" $(CFG_INC) "$<"
LINK=g++  -g -static -Wall -o "$(OUTFILE)" $(OBJ) $(CFG_LIB)
endif


#
# Configuration: Release
#
ifeq "$(CFG)" "Release"
OUTDIR=Release
OUTFILE=$(OUTDIR)/chroma
CFG_INC=
CFG_LIB=-lstdc++
CFG_OBJ=

COMPILE=g++ -c  -pipe -DNDEBUG -O2 -Wall -o "$(OUTDIR)/$(*F).o" $(CFG_INC) "$<"
LINK=g++ -pipe -O2  -static -Wall -o "$(OUTFILE)" $(OBJ) $(CFG_LIB)
endif



#
# object files
#
COMMON_OBJ=$(OUTDIR)/annotateoptions.o $(OUTDIR)/annotatesequence.o \
	$(OUTDIR)/comboformattedoutput.o $(OUTDIR)/console_main.o \
	$(OUTDIR)/formattedoutput.o $(OUTDIR)/htmlformattedoutput.o \
	$(OUTDIR)/nonmfc_stdcpp.o $(OUTDIR)/parse.o $(OUTDIR)/parseclustal.o \
	$(OUTDIR)/parsefasta.o $(OUTDIR)/parsemsf.o $(OUTDIR)/parsephd.o \
	$(OUTDIR)/perlutil.o $(OUTDIR)/residuegroup.o \
	$(OUTDIR)/rtfformattedoutput.o $(OUTDIR)/sequence.o
OBJ=$(COMMON_OBJ) $(CFG_OBJ)




# Pattern rules
#
$(OUTDIR)/%.o : %.cpp
	$(COMPILE)

$(OUTDIR)/%.d : %.cpp
	@mkdir -p $(OUTDIR)
	@g++ -MM $(CFG_INC) $< | sed 's|\($*\)\.o[ :]*|$(OUTDIR)/\1.o $@ : |g' > $@

$(OUTDIR)/%.o : common/%.cpp
	$(COMPILE)

$(OUTDIR)/%.d : common/%.cpp
	@mkdir -p $(OUTDIR)
	@g++ -MM $(CFG_INC) $< | sed 's|\($*\)\.o[ :]*|$(OUTDIR)/\1.o $@ : |g' > $@

$(OUTDIR)/%.o : components/%.cpp
	$(COMPILE)

$(OUTDIR)/%.d : components/%.cpp
	@mkdir -p $(OUTDIR)
	@g++ -MM $(CFG_INC) $< | sed 's|\($*\)\.o[ :]*|$(OUTDIR)/\1.o $@ : |g' > $@

#
# Build rules
#
all: $(OUTFILE)

$(OUTFILE): $(OUTDIR)  $(OBJ)
	$(LINK)

$(OUTDIR):
	mkdir -p "$(OUTDIR)"

-include $(OBJ:.o=.d)


#
# Build and test
#
test: $(OUTFILE)
	@$(OUTFILE) chroma.cft chroma.opt sample.clu > test.html
	@$(OUTFILE) --rtf chroma.cft chroma.opt sample.clu > test.rtf
	@echo
	@cmp test.html sample.html
	@cmp test.rtf sample.rtf
	@rm test.html
	@rm test.rtf
	@echo "	CHROMA successfully built at [ $(OUTFILE) ]"
	@echo


#
# enumerate config files
#
enumerate: $(OUTFILE)
	@$(OUTFILE) --rtf alternative_cft_files/cppbw.cft                         chroma.opt sample.clu > alternative_cft_files/cppbw.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default.cft                       chroma.opt sample.clu > alternative_cft_files/default.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default_singles.cft               chroma.opt sample.clu > alternative_cft_files/default_singles.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default_with_singles_and_turn.cft chroma.opt sample.clu > alternative_cft_files/default_with_singles_and_turn.rtf
	@$(OUTFILE) --rtf alternative_cft_files/cppcol.cft                        chroma.opt sample.clu > alternative_cft_files/cppcol.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default_no_singles.cft            chroma.opt sample.clu > alternative_cft_files/default_no_singles.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default_singles_with_turn.cft     chroma.opt sample.clu > alternative_cft_files/default_singles_with_turn.rtf
	@$(OUTFILE) --rtf alternative_cft_files/default_with_turn.cft             chroma.opt sample.clu > alternative_cft_files/default_with_turn.rtf
	@$(OUTFILE) alternative_cft_files/cppbw.cft                         chroma.opt sample.clu > alternative_cft_files/cppbw.html
	@$(OUTFILE) alternative_cft_files/default.cft                       chroma.opt sample.clu > alternative_cft_files/default.html
	@$(OUTFILE) alternative_cft_files/default_singles.cft               chroma.opt sample.clu > alternative_cft_files/default_singles.html
	@$(OUTFILE) alternative_cft_files/default_with_singles_and_turn.cft chroma.opt sample.clu > alternative_cft_files/default_with_singles_and_turn.html
	@$(OUTFILE) alternative_cft_files/cppcol.cft                        chroma.opt sample.clu > alternative_cft_files/cppcol.html
	@$(OUTFILE) alternative_cft_files/default_no_singles.cft            chroma.opt sample.clu > alternative_cft_files/default_no_singles.html
	@$(OUTFILE) alternative_cft_files/default_singles_with_turn.cft     chroma.opt sample.clu > alternative_cft_files/default_singles_with_turn.html
	@$(OUTFILE) alternative_cft_files/default_with_turn.cft             chroma.opt sample.clu > alternative_cft_files/default_with_turn.html
	@echo "	The results of different configuration files are saved in the alternative_cft_files directory"
	@echo

#
# Rebuild this project
#
rebuild: cleanall all



#
# Clean this project
#
clean:
	rm -f $(OUTFILE)
	rm -f $(OBJ)

#
# Clean this project and all dependencies
#
cleanall: clean
